package com.gmrz.uap.equipment;

import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.ServiceSupport;
import com.gmrz.uap.model.*;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.log4j.Logger;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EquipmentService extends ServiceSupport {
    public final static EquipmentService me = new EquipmentService();

    private final static Logger LOG = Logger.getLogger(EquipmentService.class);


    public boolean vendorExist(String vendorName) {
        Vendor vendor = Vendor.dao.getByVendorName(vendorName);
        return null != vendor;
    }



    public Page<Vendor> findVendorListForPaginate(Vendor vendor, int page, int rows, String sort, String order) {
        return Vendor.dao.findListForPaginate(vendor, page, rows, sort, order);
    }

    public Page<Vendor> findVendorListForPaginateNotInAllow(Vendor vendor, String tenantId, int page, int rows, String sort, String order) {
        return Vendor.dao.findListForPaginateNotInAllow(vendor, tenantId, page, rows, sort, order);
    }

    public Page<VendorAllow> findVendorAllowListForPaginate(VendorAllow vendorAllow, int page, int rows, String sort, String order) {
        return VendorAllow.dao.findListForPaginate(vendorAllow, page, rows, sort, order);
    }


    /**
     * 根据参数删除厂商信息，ID可以是多个，中间使用逗号隔开
     *
     * @param ids ID集合
     * @return true/false
     */
    @Before(Tx.class)
    public boolean vendorRemove(String ids) {

        String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);

        if (idArr.length == 0) {
            return false;
        }

        for (String id : idArr) {

            flag = Vendor.dao.deleteById(id);
            if (!flag) {
                break;
            }
        }

        return flag;
    }


    public List<Vendor> findVendorList() {
        return Vendor.dao.findAll();
    }


    public boolean exist(String equModel, String venId) {
        Equipment equipment = Equipment.dao.getByEquModel(equModel, venId);
        return null != equipment;
    }


    public Page<Equipment> findEquipmentListForPaginate(Equipment equipment, int page, int rows, String sort, String order) {
        return Equipment.dao.findListForPaginate(equipment, page, rows, sort, order);
    }


    public Page<Equipment> findListForPaginateNotInForbid(Equipment equipment, String tenantId, int page, int rows, String sort, String order) {
        return Equipment.dao.findListForPaginateNotInForbid(equipment, tenantId, page, rows, sort, order);
    }

    public Page<EquipmentForbid> findEquipmentForbidListForPaginate(EquipmentForbid equipmentForbid, int page, int rows, String sort, String order) {
        return EquipmentForbid.dao.findListForPaginate(equipmentForbid, page, rows, sort, order);
    }

    public Page<DeviceForbid> findDeviceForbidListForPaginate(DeviceForbid deviceForbid, int page, int rows, String sort, String order) {
        return DeviceForbid.dao.findListForPaginate(deviceForbid, page, rows, sort, order);
    }

    /**
     * 根据参数删除设备信息，ID可以是多个，中间使用逗号隔开
     *
     * @param ids ID集合
     * @return true/false
     */
    @Before(Tx.class)
    public boolean remove(String ids) {

        String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);

        if (idArr.length == 0) {
            return false;
        }

        for (String id : idArr) {

            flag = Equipment.dao.deleteById(id);
            if (!flag) {
                break;
            }
            Equipment.dao.deleteVersionByEquId(id);
        }

        return flag;
    }


    /**
     * 检测厂商是否被使用
     *
     * @param venId
     * @return
     */
    public boolean checkVendorIsUse(String venId) {
        long vendorInEquCount = Equipment.dao.findCountByVenId(venId);
        long vendorAllowCount = VendorAllow.dao.findCountByVendorID(venId);//厂商白名单
        long equipmentForbidCount = EquipmentForbid.dao.findCountByVendorID(venId);//型号黑名单

        return vendorInEquCount > 0 || vendorAllowCount > 0 || equipmentForbidCount > 0;
    }


    public boolean checkEquipmentIsUse(String equId) {
        long equipmentForbidCount = EquipmentForbid.dao.findCountByEquID(equId);
        long equipmentAllowCount = VendorAllow.dao.findCountByEquID(equId);
        return equipmentForbidCount > 0 || equipmentAllowCount> 0;
    }



    public boolean checkEquipmentVersionIsUse(String equVerId) {
        long equipmentForbidCount = EquipmentForbid.dao.findCountByEquVerID(equVerId);
        long equipmentAllowCount = VendorAllow.dao.findCountByEquVerID(equVerId);
        return equipmentForbidCount > 0 || equipmentAllowCount> 0;
    }



    public boolean deviceIdExist(String deviceId, String tenantId) {
        return DeviceForbid.dao.findByDeviceID(deviceId, tenantId) != null;
    }

    /**
     * 根据参数删除设备标识信息，ID可以是多个，中间使用逗号隔开
     *
     * @param ids ID集合
     * @return true/false
     */
    @Before(Tx.class)
    public boolean deviceForbidRemove(String ids) {

        String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);

        if (idArr.length == 0) {
            return false;
        }

        for (String id : idArr) {

            flag = DeviceForbid.dao.deleteById(id);
            if (!flag) {
                break;
            }
        }

        return flag;
    }

    public Page<Record> deviceVersionList(int page, int rows, String sort, String order) {
        return Equipment.dao.deviceVersionList(page, rows, sort, order);
    }



    public boolean vendorIfExistForibAllow(String venID, String tenantID) {
        List<VendorAllow> allowList = VendorAllow.dao.getAllowByVenID(venID);
        List<EquipmentForbid> foribList = EquipmentForbid.dao.getForibByVenID(venID);
        Set<String> tenantids = new HashSet<>();
        if (allowList!=null && allowList.size()>0){
            for(VendorAllow va : allowList){
                tenantids.add(va.getTenantId());
            }
        }
        if (foribList!=null && foribList.size()>0){
            for(EquipmentForbid ef : foribList){
                tenantids.add(ef.getTenantId());
            }
        }
        if ((tenantids.size() == 0) ||
                (tenantids.size() == 1 && tenantids.contains(tenantID)) ){
            return true;
        }
        return false;
    }




    public boolean equipmentIfExistForibAllow(String equID, String tenantID) {
        List<VendorAllow> allowList = VendorAllow.dao.getAllowByEquID(equID);
        List<EquipmentForbid> foribList = EquipmentForbid.dao.getForibByEquID(equID);
        Set<String> tenantids = new HashSet<>();
        if (allowList!=null && allowList.size()>0){
            for(VendorAllow va : allowList){
                tenantids.add(va.getTenantId());
            }
        }
        if (foribList!=null && foribList.size()>0){
            for(EquipmentForbid ef : foribList){
                tenantids.add(ef.getTenantId());
            }
        }
        if ((tenantids.size() == 0) ||
                (tenantids.size() == 1 && tenantids.contains(tenantID)) ){
            return true;
        }
        return false;
    }



    public boolean equipmentVersionIfExistForibAllow(String equVerID, String tenantID) {
        List<VendorAllow> allowList = VendorAllow.dao.getAllowByEquVerID(equVerID);
        List<EquipmentForbid> foribList = EquipmentForbid.dao.getForibByEquVerID(equVerID);
        Set<String> tenantids = new HashSet<>();
        if (allowList!=null && allowList.size()>0){
            for(VendorAllow va : allowList){
                tenantids.add(va.getTenantId());
            }
        }
        if (foribList!=null && foribList.size()>0){
            for(EquipmentForbid ef : foribList){
                tenantids.add(ef.getTenantId());
            }
        }
        if ((tenantids.size() == 0) ||
                (tenantids.size() == 1 && tenantids.contains(tenantID)) ){
            return true;
        }
        return false;
    }

}
